<?php
/********************
处理图片的类
lsj 2008-09-11
*********************/

class lib_Class_Image
{
	private $w_pct = 100;#水印图片的透明度
	private $w_quality = 80;#水印图片质量，最小越失真，图片大小也会最小
	private $interlace = 0;#水印图片交织交错值，一般为0

	private $w_minwidth = 300;#需加水印的图片的最小宽
	private $w_minheight = 300;#需加水印的图片的最小高


	function __construct() {

	}

	/*
	#设置加水印的最小宽高
	*/
	function set_min($w, $h)
	{
		$this->w_minwidth = $w;
		$this->w_minheight = $h;
		return true;
	}

	/*
	#设置加水印的一些参数
	*/
	function set_param($pct, $quality, $interlace)
	{
		$this->w_pct = $pct;
		$this->w_quality = $quality;
		$this->interlace = $interlace;
		return true;
	}

	/*
	#取图片的基本信息
	*/
	function get_info($image)
	{
		if(!$this->check($image)) return false;
        $imageinfo = getimagesize($image);
        if($imageinfo === false) return false;
		$imagetype = strtolower(substr(image_type_to_extension($imageinfo[2]),1));
		$imagesize = filesize($image);
		$info = array(
				'width'=>$imageinfo[0],
				'height'=>$imageinfo[1],
				'type'=>$imagetype,
				'size'=>$imagesize,
				'mime'=>$imageinfo['mime']
				);
		return $info;
    }

	/*
	#取图片的EXIF信息
	*/
	function get_exif($image)
	{
		if(!$this->check($image)) return false;

		$exif = @exif_read_data($image,'IFD0');

		/*主要信息说明
		$exif['Make'];#相机制造商
		$exif['Model'];#相机型号

		$exif['FocalLength'];#焦距
		$exif['FileDateTime'];#时间戳

		$exif['MaxApertureValue'];#最大光圈值
		$exif['ShutterSpeedValue'];#快门速度
		$exif['COMPUTED']['ApertureFNumber'];#快门光圈
		$exif['ExposureTime'];#曝光时间
		$exif['ExposureMode'];#曝光模式 1：手动
		$exif['WhiteBalance'];#白平衡 1：手动

		$exif['Flash'];#闪光灯
		$exif['MeteringMode'];#测光模式
		$exif['LightSource'];#光源
		*/

		return $exif;
	}

	/*
	#压图
	$image:原图的文件路径
	$filename: 生成的新图的文件名
	$maxwidth: 压缩的最大宽度
	$maxheight: 压缩的最大高度
	$autocut: 是否按最大的宽高比进行剪切
	*/
    function thumb($image, $filename = '', $maxwidth = 200, $maxheight = 150, $autocut = 0)
    {
		if(!$this->check($image)) return false;
        $info  = image::get_info($image);
        if($info === false) return false;
		$srcwidth  = $info['width'];
		$srcheight = $info['height'];
		$pathinfo = pathinfo($image);
		$type =  $pathinfo['extension'];
		if(!$type) $type = $info['type'];
		$type = strtolower($type);
		unset($info);

		$scale = min($maxwidth/$srcwidth, $maxheight/$srcheight);
		$createwidth = $width  = (int)($srcwidth*$scale);
		$createheight = $height = (int)($srcheight*$scale);
		if($maxwidth >= $srcwidth) $createwidth = $width = $srcwidth;
		if($maxheight >= $srcheight) $createheight = $height = $srcheight;
		$psrc_x = $psrc_y = 0;
		if($autocut) {
			if($maxwidth/$maxheight<$srcwidth/$srcheight && $maxheight>=$height) {
				$width = $maxheight/$height*$width;
				$height = $maxheight;
			} elseif($maxwidth/$maxheight>$srcwidth/$srcheight && $maxwidth>=$width) {
				$height = $maxwidth/$width*$height;
				$width = $maxwidth;
			}
			$createwidth = $maxwidth;
			$createheight = $maxheight;
		}
		$createfun = 'imagecreatefrom'.($type=='jpg' ? 'jpeg' : $type);
		$srcimg = $createfun($image);
		if($type != 'gif' && function_exists('imagecreatetruecolor'))
			$thumbimg = imagecreatetruecolor($createwidth, $createheight);
		else
			$thumbimg = imagecreate($width, $height);

		if(function_exists('imagecopyresampled'))
			imagecopyresampled($thumbimg, $srcimg, 0, 0, $psrc_x, $psrc_y, $width, $height, $srcwidth, $srcheight);
		else
			imagecopyresized($thumbimg, $srcimg, 0, 0, $psrc_x, $psrc_y, $width, $height,  $srcwidth, $srcheight);
		if($type=='gif' || $type=='png') {
			$background_color  =  imagecolorallocate($thumbimg,  0, 255, 0);  //  指派一个绿色
			imagecolortransparent($thumbimg, $background_color);  //  设置为透明色，若注释掉该行则输出绿色的图
		}
		if($type=='jpg' || $type=='jpeg') imageinterlace($thumbimg, $this->interlace);
		$imagefun = 'image'.($type=='jpg' ? 'jpeg' : $type);
		if(empty($filename)) $filename  = substr($image, 0, strrpos($image, '.')).'_thumb.'.$type;
		$imagefun($thumbimg, $filename);
		imagedestroy($thumbimg);
		imagedestroy($srcimg);
		return $filename;
    }

	/*
	#加水印
	$source: 原图的文件路径
	$target: 生成的新图的文件路径
	$w_pos: 水印所处的位置
	$w_img: 水印图片
	$w_text: 水印文字
	$w_font: 水印文字大小
	$w_color: 水印文字颜色
	*/
	function watermark($source, $target = '', $w_pos = 0, $w_img = '', $w_text = '', $w_font = 5, $w_color = '#ff0000')
	{
		if(!$this->check($source)) return false;
		if(!$target) $target = $source;
		$source_info = getimagesize($source);
		$source_w    = $source_info[0];
		$source_h    = $source_info[1];

		//当图片尺寸小于加水印的最小宽高
		if($source_w < $this->w_minwidth || $source_h < $this->w_minheight) return false;

		switch($source_info[2]) {
			case 1 :
				$source_img = imagecreatefromgif($source);
				break;
			case 2 :
				$source_img = imagecreatefromjpeg($source);
				break;
			case 3 :
				$source_img = imagecreatefrompng($source);
				break;
			default :
				return false;
		}
		if(!empty($w_img) && file_exists($w_img)) {
			$ifwaterimage = 1;
			$water_info   = getimagesize($w_img);
			$width        = $water_info[0];
			$height       = $water_info[1];
			switch($water_info[2]) {
				case 1 :
					$water_img = imagecreatefromgif($w_img);
					break;
				case 2 :
					$water_img = imagecreatefromjpeg($w_img);
					break;
				case 3 :
					$water_img = imagecreatefrompng($w_img);
					break;
				default :
					return;
			}
		} else {
			$ifwaterimage = 0;
			//取得使用 truetype 字体的文本的范围
			$temp = imagettfbbox(ceil($w_font*2.5), 0, 'fonts/cour.ttf', $w_text);
			$width = $temp[2] - $temp[6];
			$height = $temp[3] - $temp[7];
			unset($temp);
		}
		switch($w_pos) {
			case 0:#随机
				$wx = rand(0,($source_w - $width));
				$wy = rand(0,($source_h - $height));
				break;
			case 1:#左上边
				$wx = 5;
				$wy = 5;
				break;
			case 2:#中上边
				$wx = ($source_w - $width) / 2;
				$wy = 0;
				break;
			case 3:#右上边
				$wx = $source_w - $width;
				$wy = 0;
				break;
			case 4:#右中边
				$wx = 0;
				$wy = ($source_h - $height) / 2;
				break;
			case 5:#中中边
				$wx = ($source_w - $width) / 2;
				$wy = ($source_h - $height) / 2;
				break;
			case 6:#左中边
				$wx = $source_w - $width;
				$wy = ($source_h - $height) / 2;
				break;
			case 7:#左下边
				$wx = 0;
				$wy = $source_h - $height;
				break;
			case 8:#中下边
				$wx = ($source_w - $width) / 2;
				$wy = $source_h - $height;
				break;
			case 9:#右下边
				$wx = $source_w - $width;
				$wy = $source_h - $height;
				break;
			default:#随机
				$wx = rand(0,($source_w - $width));
				$wy = rand(0,($source_h - $height));
				break;
		}
		if($ifwaterimage) {
			imagecopymerge($source_img, $water_img, $wx, $wy, 0, 0, $width, $height, $this->w_pct);
		} else {
			if(!empty($w_color) && (strlen($w_color)==7)) {
				$r = hexdec(substr($w_color,1,2));
				$g = hexdec(substr($w_color,3,2));
				$b = hexdec(substr($w_color,5));
			} else {
				return;
			}
			imagestring($source_img,$w_font,$wx,$wy,$w_text,imagecolorallocate($source_img,$r,$g,$b));
		}
		switch($source_info[2]) {
			case 1 :
				imagegif($source_img, $target);
				break;
			case 2 :
				imagejpeg($source_img, $target, $this->w_quality);
				break;
			case 3 :
				imagepng($source_img, $target);
				break;
			default :
				return;
		}
		if(isset($water_info)) {
			unset($water_info);
		}
		if(isset($water_img)) {
			imagedestroy($water_img);
		}
		unset($source_info);
		imagedestroy($source_img);
		return true;
	}

	/*
	#对图片进行检查
	检查是否加载GD库
	检查图片是否存在
	检查图片创建函数是否存在
	*/
	function check($image)
	{
		return extension_loaded('gd') && preg_match("/\.(jpg|jpeg|gif|png)/i", $image, $m) && file_exists($image) && function_exists('imagecreatefrom'.($m[1] == 'jpg' ? 'jpeg' : $m[1]));
	}

}



?>